﻿namespace Stopwatch
{
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.ComponentModel;

    public sealed class StopedItemModel : IEnumerable<StopedItem>, INotifyCollectionChanged
    {
        List<StopedItem> items;

        public StopedItemModel()
        {
            items = new List<StopedItem>();
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public IEnumerator<StopedItem> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(StopedItem item)
        {
            item.Order = items.Count + 1;
            items.Add(item);

            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, item.Order - 1));
            }
        }

        public void Clear()
        {
            items.Clear();

            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }
    }

    public class StopedItem : INotifyPropertyChanged
    {
        public StopedItem(Time time)
        {
            stopedTime = time.ToWatch();
        }

        private int order;

        public int Order
        {
            get
            {
                return order;
            }
            set
            {
                if (value != order)
                {
                    order = value;
                    NotifyPropertyChanged("Order");
                }
            }
        }

        private string stopedTime;

        public string StopedTime
        {
            get
            {
                return stopedTime;
            }
            set
            {
                if (value != stopedTime)
                {
                    stopedTime = value;
                    NotifyPropertyChanged("StopedTime");
                }
            }
        }

        private string originalTime;

        public string OriginalTime
        {
            get
            {
                return originalTime;
            }
            set
            {
                if (value != originalTime)
                {
                    originalTime = value;
                    NotifyPropertyChanged("OriginalTime");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}